﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using TheGameClient.Webservices;
using TheGameClient.WorldMap;

namespace TheGameClient
{
	public partial class DlgMainMapWindow
	{
		private List<ulong> _connectedNodes;
		private List<SmallComputerSystem> _knownSystems;

		public DlgMainMapWindow()
		{
			InitializeComponent();
		}

		public List<ulong> ConnectedNodes
		{
			get { return _connectedNodes; }
		}

		public SmallComputerSystem TargetSystem { get; set; }

		//protected override Size ArrangeOverride(Size finalSize)
		//{
		//    return base.ArrangeOverride(finalSize);
		//    double width = finalSize.Width;
		//    double height = width/2;
		//    Size correctedSize = new Size(width, height);
		//    return base.ArrangeOverride(correctedSize);
		//}

		private void OkButtonClick(object sender, RoutedEventArgs e)
		{
			_connectedNodes = worldMap.ConnectedNodes;
			if (_connectedNodes.Count == 0)
				return;
			TargetSystem = (from i in App.MyKnownComputers where i.Id == _connectedNodes.Last() select i).FirstOrDefault();
			DialogResult = true;
		}

		private void CancelButtonClick(object sender, RoutedEventArgs e)
		{
			DialogResult = false;
		}

		private void BtnSaveConnectionClick(object sender, RoutedEventArgs e)
		{
			List<ulong> connectedSystems = (from s in worldMap.ConnectedNodes select s).ToList();
			DlgSaveConnection dlg = new DlgSaveConnection(connectedSystems);
			dlg.Closed += delegate
			{
				if (dlg.DialogResult != true)
					return;
			};
			dlg.Show();
		}

		private void BtnLoadConnectionClick(object sender, RoutedEventArgs e)
		{
			DlgLoadConnection dlg = new DlgLoadConnection();
			dlg.Closed += delegate
			{
				if (dlg.DialogResult != true)
					return;
				worldMap.ConnectedNodes.Clear();
				worldMap.connectionCanvas.Children.Clear();
				SavedConnection selectedConnection = dlg.SelectedConnection;
				for (int i = 0; i < selectedConnection.Route.Count; i++)
				{
					if (i == 0)
					{
						NetworkNode home =
							(from nwn in worldMap.nodeCanvas.Children.OfType<NetworkNode>() where nwn.IsHomeSystem select nwn).FirstOrDefault
								();
						NetworkNode next = (from nwn in worldMap.nodeCanvas.Children.OfType<NetworkNode>()
						                    where nwn.ComputerSystemId == selectedConnection.Route[i]
						                    select nwn).FirstOrDefault();
						if (home == null)
							throw new Exception("Error: No home system on map.");
						if (next == null)
							throw new Exception("Error: connection appears to be invalid.");
						worldMap.CreateConnection(home, next);
					}
					else
					{
						NetworkNode previous = (from nwn in worldMap.nodeCanvas.Children.OfType<NetworkNode>()
						                        where nwn.ComputerSystemId == selectedConnection.Route[i - 1]
						                        select nwn).FirstOrDefault();
						NetworkNode next = (from nwn in worldMap.nodeCanvas.Children.OfType<NetworkNode>()
						                    where nwn.ComputerSystemId == selectedConnection.Route[i]
						                    select nwn).FirstOrDefault();
						if (previous == null || next == null)
							throw new Exception("Error: connection appears to be invalid.");
						worldMap.CreateConnection(previous, next);
					}
				}
			};
			dlg.Show();
		}
	}
}